Note: this document is meant to be a high-level overview of constructing a dashboard using flexdashboard, plotly and crosstalk. This is not a definitive guide to each package nor is this the only approach to constructing a dashboard using R. Please refer to the links provided for more details on how to use the packages highlighted here.
Source: https://plotly-r.com
---
title: "flexdashboard + plotly + crosstalk"
output: flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
library(plotly)
library(crosstalk)
lw <- read.csv("length-weight_data.csv")
shared_lw <- SharedData$new(lw, ~record)
```
Column {.tabset}
-------------------------------------
### Length-Weight relationship
```{r}
plot_ly(data = shared_lw) %>%
add_markers(x = ~length, y = ~weight, name = "Observed") %>%
add_lines(x = ~length, y = ~exp(fit), name = "Predicted")
```
### Residuals vs. fitted values
```{r}
plot_ly(data = shared_lw) %>%
add_markers(x = ~fit, y = ~res)
```
This will generate a simple flexdashboard with two interactive figures (duplicated to the right)
This skeleton is a verbatim copy of an R markdown file (extension .Rmd) set-up to produce a simple flexdashboard with interactive plotly visuals connected by crosstalk. Like most R markdown files, it includes three types of content:
----markdown formatting```The YAML header includes the metadata for the file, such as the document title and output format:
---
title: "flexdashboard + plotly + crosstalk"
output: flexdashboard::flex_dashboard
---
While only a title and format was specified in the skeleton, many other options are available (e.g. author, date).
The next section is a chunk of R code:
```{r setup, include=FALSE}
library(plotly)
library(crosstalk)
lw <- read.csv("length-weight_data.csv")
shared_lw <- SharedData$new(lw, ~record)
```
This is where the plotly and crosstalk packages are loaded along with some sample data. The sample data includes length and weight data (columns length and weight, respectively) along with fitted values and residuals from a length-weight regression (columns called fit and res, respectively). The SharedData function from crosstalk package is also used here to generate a data object that can be “shared” across independent plots.
Following this chunk of R code is a level 2 markdown header that tells flexdashboard to introduce a column break and place the subsequent components into separate tabs:
Column {.tabset}
-------------------------------------
Following this break, independent tabs are defined using three hashtags followed by an optional tab name (level 3 markdown header). This header can be followed by either markdown text or R code. Here, two tabs are generated with plotly plots using shared data from crosstalk:
### Length-Weight relationship
```{r}
plot_ly(data = shared_lw) %>%
add_markers(x = ~length, y = ~weight, name = "Observed") %>%
add_lines(x = ~length, y = ~exp(fit), name = "Predicted")
```
### Residuals vs. fitted values
```{r}
plot_ly(data = shared_lw) %>%
add_markers(x = ~fit, y = ~res)
```
The native syntax of plotly was inspired by the grammar of graphics and, as such, its general structure will be familiar to those who have used ggplot2. The package has also been structured to be pipe (%>%) and dplyr friendly, making the code more intuitive and efficient. Though the script ends with the residual vs. fitted plot, the dashboard can easily be extended to include other diagnostic plots, such as a histogram of the residuals.
Once this script is “Knit” (Ctrl+Shift+K in Rstudio), a stand-alone html document will be produced with the plots rendered into independent tabs (shown to the right). Clicking a specific point in one plot will highlight the corresponding point in the other plot. In short, flexdashboard sets up the structure of the document, plotly produces the interactive figures and crosstalk connects the plots held in independent tabs.
Of course, this is only a rudimentary overview of what is possible with flexdashboard, plotly and crosstalk. A wide range of layout options are possible using flexdashboard, plotly can produce more than just scatter and line plots and crosstalk can connect various widgets. See the links provided on the Background page for more details on each package. The hope here is that this skeleton serves as a starting point from which to build more elaborate dashboards tailored to specific needs.
The package rmarkdown enables the writing of markdown text and R code in the same document (extension .Rmd). Most R markdown files includes three types of content:
----markdown formatting```The first step in setting up an R markdown file is to define a YAML header. In the skeleton, it looks like this:
---
title: "flexdashboard + plotly + crosstalk"
output: flexdashboard::flex_dashboard
---
This is where the output type is defined along with other items such as title, author, date, etc. A flex_dashboard is requested here. There are are growing number of output types supported by R markdown (e.g. html_document, pdf_document, word_document).
Next, write plain text using markdown syntax to describe how to format the text in the final document.
Plain text simply translates to Plain text in the default font of the output document.*italic* or _italic_ \(\rightarrow\) italic**bold** or __bold__ \(\rightarrow\) bold**_bold-italic_** \(\rightarrow\) bold-italic$W = \alpha L ^{\beta}$ \(\rightarrow\) \(W = \alpha L ^{\beta}\)1. ordered item 1 \(\rightarrow\) 1. ordered item 1- unordered item \(\rightarrow\) • unordered item##### Header 5 \(\rightarrow\)
flexdashboardFinally, mix in R code by surrounding chunks of code using one backtick for inline code (e.g.`r 1+1` will print 2 in the output) or three backticks to run several lines of code and/or display a table or plot. The below chunk will print the top three rows of the cars data-set:
```{r}
head_cars <- head(cars, 3)
head_cars
```
Resulting in the following output:
speed dist
1 4 2
2 4 10
3 7 4
---
title: "Dashboards for stock assessments: Getting started"
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
---
```{r setup, include=FALSE}
library(plotly)
library(crosstalk)
```
Background
=======================================================================
Row {data-height=300}
-----------------------------------------------------------------------
### Dashboards
- Browser-based dashboards and interactive visualizations are becoming increasingly common and accessible
- With a relatively shallow learning-curve, an [**R**](https://www.r-project.org/) user can use the [**flexdashboard**](https://rmarkdown.rstudio.com/flexdashboard/), [**plotly**](https://plotly-r.com) and [**crosstalk**](https://rstudio.github.io/crosstalk/) packages to generate interactive dashboards for exploring data and models
- The concept is demonstrated on the [Skeleton](#skeleton) page using a simple data-set and linear regression; this can be extended to more complex cases
> Note: this document is meant to be a high-level overview of constructing a dashboard using [**flexdashboard**](https://rmarkdown.rstudio.com/flexdashboard/), [**plotly**](https://plotly-r.com) and [**crosstalk**](https://rstudio.github.io/crosstalk/). This is not a definitive guide to each package nor is this the only approach to constructing a dashboard using R. Please refer to the links provided for more details on how to use the packages highlighted here.
Row {data-height=500}
-----------------------------------------------------------------------
### flexdashboard
- Uses [**rmarkdown**](https://rmarkdown.rstudio.com/) to render a group of related figures, tables and text into a dashboard
- Layout is flexible and the components automatically re-size to fill the browser and adapt to mobile displays
- Supports a wide range of components, including base plot, ggplots, gauges, tables and [htmlwidgets](http://www.htmlwidgets.org/index.html) such as [**plotly**](https://plotly-r.com)
- Optionally use [**shiny**](http://shiny.rstudio.com/) or [**crosstalk**](https://rstudio.github.io/crosstalk/) to bolster interactivity
> Source: https://rmarkdown.rstudio.com/flexdashboard/
### plotly
- A graphing package that works like other R plots except it produces interactive visualizations
- The package allows the user to create interactive web graphics from [**ggplot2**](https://ggplot2.tidyverse.org/) graphs
- Also provides a more 'direct' link to the core [plotly.js](https://plot.ly/javascript/) JavaScript library using syntax inspired by the grammar of graphics
> Source: https://plotly-r.com
### crosstalk
- Enables cross-widget interactions by linking brushing and/or filtering across multiple views
- i.e. Interactions with one plot can affect change in another plot
- Supports a wide range of [htmlwidgets](http://www.htmlwidgets.org/index.html), such as [**plotly**](https://plotly-r.com) and [**leaflet**](https://rstudio.github.io/leaflet/)
> Source: https://rstudio.github.io/crosstalk/
Skeleton {data-orientation=columns}
=======================================================================
Column {.tabset}
-------------------------------------
### Code
```{r echo = FALSE, comment = ""}
cat(htmltools::includeText("skeleton.Rmd"))
```
> This will generate a simple flexdashboard with two interactive figures (duplicated to the right)
### Details
This skeleton is a verbatim copy of an R markdown file (extension `.Rmd`) set-up to produce a simple `flexdashboard` with interactive `plotly` visuals connected by `crosstalk`. Like most R markdown files, it includes three types of content:
1. A YAML header surrounded by `----`
2. Text following `markdown` formatting
3. R code chunks surrounded by `` ``` ``
The YAML header includes the metadata for the file, such as the document title and output format:
````
---
title: "flexdashboard + plotly + crosstalk"
output: flexdashboard::flex_dashboard
---
````
While only a title and format was specified in the skeleton, many other options are available (e.g. author, date).
The next section is a chunk of R code:
````
```{r setup, include=FALSE} `r ''`
library(plotly)
library(crosstalk)
lw <- read.csv("length-weight_data.csv")
shared_lw <- SharedData$new(lw, ~record)
```
````
This is where the `plotly` and `crosstalk` packages are loaded along with some sample data. The sample data includes length and weight data (columns `length` and `weight`, respectively) along with fitted values and residuals from a length-weight regression (columns called `fit` and `res`, respectively). The `SharedData` function from `crosstalk` package is also used here to generate a data object that can be "shared" across independent plots.
Following this chunk of R code is a level 2 markdown header that tells flexdashboard to introduce a column break and place the subsequent components into separate tabs:
````
Column {.tabset}
-------------------------------------
````
Following this break, independent tabs are defined using three hashtags followed by an optional tab name (level 3 markdown header). This header can be followed by either markdown text or R code. Here, two tabs are generated with `plotly` plots using shared data from `crosstalk`:
````
### Length-Weight relationship
```{r} `r ''`
plot_ly(data = shared_lw) %>%
add_markers(x = ~length, y = ~weight, name = "Observed") %>%
add_lines(x = ~length, y = ~exp(fit), name = "Predicted")
```
### Residuals vs. fitted values
```{r} `r ''`
plot_ly(data = shared_lw) %>%
add_markers(x = ~fit, y = ~res)
```
````
The native syntax of `plotly` was inspired by the grammar of graphics and, as such, its general structure will be familiar to those who have used `ggplot2`. The package has also been structured to be pipe (`%>%`) and `dplyr` friendly, making the code more intuitive and efficient. Though the script ends with the residual vs. fitted plot, the dashboard can easily be extended to include other diagnostic plots, such as a histogram of the residuals.
Once this script is "Knit" (`Ctrl+Shift+K` in Rstudio), a stand-alone html document will be produced with the plots rendered into independent tabs (shown to the right). Clicking a specific point in one plot will highlight the corresponding point in the other plot. In short, `flexdashboard` sets up the structure of the document, `plotly` produces the interactive figures and `crosstalk` connects the plots held in independent tabs.
Of course, this is only a rudimentary overview of what is possible with `flexdashboard`, `plotly` and `crosstalk`. A wide range of layout options are possible using `flexdashboard`, `plotly` can produce more than just scatter and line plots and `crosstalk` can connect various widgets. See the links provided on the [Background](#background) page for more details on each package. The hope here is that this skeleton serves as a starting point from which to build more elaborate dashboards tailored to specific needs.
Column {.tabset}
-------------------------------------
```{r}
lw <- read.csv("length-weight_data.csv")
shared_lw <- SharedData$new(lw, ~record)
```
### Length-Weight relationship
```{r}
plot_ly(data = shared_lw) %>%
add_markers(x = ~length, y = ~weight, name = "Observed") %>%
add_lines(x = ~length, y = ~exp(fit), name = "Predicted")
```
### Residuals vs. fitted values
```{r}
plot_ly(data = shared_lw) %>%
add_markers(x = ~fit, y = ~res)
```
rmarkdown {data-navmenu="Basics"}
=======================================================================
Row {data-height=250}
-------------------------------------
### R markdown
The package `rmarkdown` enables the writing of `markdown` text and `R` code in the same document (extension `.Rmd`). Most R markdown files includes three types of content:
1. A YAML header surrounded by `----`
2. Text following `markdown` formatting
3. R code chunks surrounded by `` ``` ``
Row {data-height=600}
-------------------------------------
### YAML
The first step in setting up an R markdown file is to define a YAML header. In the [skeleton](#skeleton), it looks like this:
````
---
title: "flexdashboard + plotly + crosstalk"
output: flexdashboard::flex_dashboard
---
````
This is where the output type is defined along with other items such as title, author, date, etc. A `flex_dashboard` is requested here. There are are growing number of output types supported by R markdown (e.g. html_document, pdf_document, word_document).
### Markdown
Next, write plain text using markdown syntax to describe how to format the text in the final document.
- `Plain text` simply translates to Plain text in the default font of the output document.
- Italics and bold are specified using asterisk and/or underscores:
- `*italic*` or `_italic_` $\rightarrow$ *italic*
- `**bold**` or `__bold__` $\rightarrow$ **bold**
- `**_bold-italic_** ` $\rightarrow$ **_bold-italic_**
- Equations can be specified using LaTeX syntax:
- `$W = \alpha L ^{\beta}$` $\rightarrow$ $W = \alpha L ^{\beta}$
- Ordered and unordered lists are generated using numbers and dashes or asterisk, respectively:
- `1. ordered item 1` $\rightarrow$ 1. ordered item 1
- `- unordered item` $\rightarrow$ • unordered item
- Headers are define using hashtags:
- `##### Header 5` $\rightarrow$ Header 5
- Note that markdown headers with three hashtags or less have specific formatting functions in `flexdashboard`
### R
Finally, mix in `R` code by surrounding chunks of code using one backtick for inline code (e.g.`` `r knitr::inline_expr("1+1")` `` will print 2 in the output) or three backticks to run several lines of code and/or display a table or plot. The below chunk will print the top three rows of the `cars` data-set:
````
```{r} `r ''`
head_cars <- head(cars, 3)
head_cars
```
````
Resulting in the following output:
```{r}
head_cars <- head(cars, 3)
head_cars
```